package com.empire.module.sporttery.controller.app.football.vo;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.empire.module.sporttery.controller.admin.footballmatch.vo.HalfScoreBallVo;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import java.util.*;
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDateTime;
import com.alibaba.excel.annotation.*;

@Schema(description = "用户 APP - 竞猜足彩比赛 Response VO")
@Data
@ExcelIgnoreUnannotated
public class AppFootballMatchRespVO {

    @Schema(description = "主键id", requiredMode = Schema.RequiredMode.REQUIRED, example = "21480")
    @ExcelProperty("主键id")
    private Integer id;

    @Schema(description = "编号", requiredMode = Schema.RequiredMode.REQUIRED)
    @ExcelProperty("编号")
    private String number;

    @Schema(description = "500万游戏编号", example = "2219")
    @ExcelProperty("500万游戏编号")
    private String matchId;

    @Schema(description = "赛事", requiredMode = Schema.RequiredMode.REQUIRED)
    @ExcelProperty("赛事")
    private String match;

    @Schema(description = "赛事500万url", example = "https://www.iocoder.cn")
    @ExcelProperty("赛事500万url")
    private String matchUrl;

    @Schema(description = "分析")
    @ExcelProperty("分析")
    private String analysis;

    @Schema(description = "颜色", requiredMode = Schema.RequiredMode.REQUIRED)
    @ExcelProperty("颜色")
    private String color;

    @Schema(description = "开赛时间", requiredMode = Schema.RequiredMode.REQUIRED)
    @ExcelProperty("开赛时间")
    private String openTime;

    @Schema(description = "总进球赔率", requiredMode = Schema.RequiredMode.REQUIRED)
    @ExcelProperty("总进球赔率")
    private String goalOdds;

    @Schema(description = "让球赔率", requiredMode = Schema.RequiredMode.REQUIRED)
    @ExcelProperty("让球赔率")
    private String letOdds;

    @Schema(description = "半全场赔率", requiredMode = Schema.RequiredMode.REQUIRED)
    @ExcelProperty("半全场赔率")
    private String halfWholeOdds;

    @Schema(description = "不让球赔率", requiredMode = Schema.RequiredMode.REQUIRED)
    @ExcelProperty("不让球赔率")
    private String notLetOdds;

    @Schema(description = "比分赔率", requiredMode = Schema.RequiredMode.REQUIRED)
    @ExcelProperty("比分赔率")
    private String scoreOdds;

    @Schema(description = "让球", requiredMode = Schema.RequiredMode.REQUIRED)
    @ExcelProperty("让球")
    private String letBall;

    @Schema(description = "主队", requiredMode = Schema.RequiredMode.REQUIRED)
    @ExcelProperty("主队")
    private String homeTeam;

    @Schema(description = "下注状态 0 不可下注 1 可下注 2 已取消", requiredMode = Schema.RequiredMode.REQUIRED)
    @ExcelProperty("下注状态 0 不可下注 1 可下注 2 已取消")
    private String state;

    @Schema(description = "客队", requiredMode = Schema.RequiredMode.REQUIRED)
    @ExcelProperty("客队")
    private String visitingTeam;

    @Schema(description = "是否单关 0 否 1 是", requiredMode = Schema.RequiredMode.REQUIRED)
    @ExcelProperty("是否单关 0 否 1 是")
    private String isSingle;

    @Schema(description = "比赛下注截止时间", requiredMode = Schema.RequiredMode.REQUIRED)
    @ExcelProperty("比赛下注截止时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm")
    private LocalDateTime deadline;

    @Schema(description = "半全场结果")
    @ExcelProperty("半全场结果")
    private String halfFullCourt;

    @Schema(description = "开奖结果")
    @ExcelProperty("开奖结果")
    private String award;

    @Schema(description = "开奖结果集")
    @ExcelProperty("开奖结果集")
    private String[] awards;

    @Schema(description = "属于哪天的比赛时间", requiredMode = Schema.RequiredMode.REQUIRED)
    @ExcelProperty("属于哪天的比赛时间")
    private String startTime;

    @Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)
    @ExcelProperty("创建时间")
    private LocalDateTime createTime;

    @Schema(description = "比分")
    @ExcelProperty("比分")
    private String score;

    @Schema(description = "主队链接", example = "https://www.iocoder.cn")
    @ExcelProperty("主队链接")
    private String homeTeamUrl;

    @Schema(description = "客队链接", example = "https://www.iocoder.cn")
    @ExcelProperty("客队链接")
    private String visitTeamUrl;

    @Schema(description = "胜赔")
    @ExcelProperty("胜赔")
    private Double winPay;

    @Schema(description = "平赔")
    @ExcelProperty("平赔")
    private Double drawPay;

    @Schema(description = "客赔")
    @ExcelProperty("客赔")
    private Double visitPay;

    @Schema(description = "让球胜赔")
    @ExcelProperty("让球胜赔")
    private Double letWinPay;

    @Schema(description = "让球平赔")
    @ExcelProperty("让球平赔")
    private Double letDrawPay;

    @Schema(description = "让球客赔")
    @ExcelProperty("让球客赔")
    private Double letVisitPay;

    @Schema(description = "胜赔概率")
    @ExcelProperty("胜赔概率")
    private String winRate;

    @Schema(description = "平赔概率")
    @ExcelProperty("平赔概率")
    private String drawWinRate;

    @Schema(description = "客赔概率")
    @ExcelProperty("客赔概率")
    private String visitWinRate;

    @Schema(description = "让球胜赔概率")
    @ExcelProperty("让球胜赔概率")
    private String letWinRate;

    @Schema(description = "让球平赔概率")
    @ExcelProperty("让球平赔概率")
    private String letDrawWinRate;

    @Schema(description = "让球客赔概率")
    @ExcelProperty("让球客赔概率")
    private String letVisitWinRate;

    @Schema(description = "胜赔")
    @ExcelProperty("胜赔")
    private Integer winFlag;

    @Schema(description = "平赔")
    @ExcelProperty("平赔")
    private Integer drawWinFlag;

    @Schema(description = "客赔")
    @ExcelProperty("客赔")
    private Integer visitWinFlag;

    @Schema(description = "让球胜赔")
    @ExcelProperty("让球胜赔")
    private Integer letWinFlag;

    @Schema(description = "让球平赔")
    @ExcelProperty("让球平赔")
    private Integer letDrawWinFlag;

    @Schema(description = "让球客赔")
    @ExcelProperty("让球客赔")
    private Integer letVisitWinFlag;

    @Schema(description = "胜赔")
    @ExcelProperty("胜赔")
    private Integer winSelect;

    @Schema(description = "平赔")
    @ExcelProperty("平赔")
    private Integer drawWinSelect;

    @Schema(description = "客赔")
    @ExcelProperty("客赔")
    private Integer visitWinSelect;

    @Schema(description = "让球胜赔")
    @ExcelProperty("让球胜赔")
    private Integer letWinSelect;

    @Schema(description = "让球平赔")
    @ExcelProperty("让球平赔")
    private Integer letDrawWinSelect;

    @Schema(description = "让球客赔")
    @ExcelProperty("让球客赔")
    private Integer letVisitWinSelect;

    @Schema(description = "投注倾向")
    @ExcelProperty("投注倾向")
    private String trendWin;

    @Schema(description = "欧赔对比")
    @ExcelProperty("欧赔对比")
    private String ouDiff;

    @Schema(description = "热队")
    @ExcelProperty("热队")
    private String hotTeam;

    @Schema(description = "500-投注分析")
    @ExcelProperty("500-投注分析")
    private String zw;

    @Schema(description = "500-投注量大小描述")
    @ExcelProperty("500-投注量大小描述")
    private String mountDesc;

    @Schema(description = "500-投注量规模")
    @ExcelProperty("500-投注量规模")
    private String mount;

    @Schema(description = "筛选排序")
    @ExcelProperty("筛选排序")
    private Integer selectSort;

    @Schema(description = "比分预测")
    @ExcelProperty("比分预测")
    private String scoreSelect;

    @Schema(description = "能力值对比")
    @ExcelProperty("能力值对比")
    private String teamScore;

    @Schema(description = "是否显示")
    @ExcelProperty("是否显示")
    private Integer isView;

    @Schema(description = "进球数指数")
    @ExcelProperty("进球数指数")
    private String ballNumPredict;

    @Schema(description = "进球数")
    @ExcelProperty("进球数")
    private String ballNum;

    @Schema(description = "欧赔主胜概率")
    @ExcelProperty("欧赔主胜概率")
    private String ouHomeRate;

    @Schema(description = "欧赔平赔概率")
    @ExcelProperty("欧赔平赔概率")
    private String ouDrawRate;

    @Schema(description = "欧赔主负概率")
    @ExcelProperty("欧赔主负概率")
    private String ouVisitRate;

    @Schema(description = "欧赔差异")
    @ExcelProperty("欧赔差异")
    private String ouResult;

    @Schema(description = "主队半场进球数-算法值")
    @ExcelProperty("主队半场进球数-算法值")
    private String homeAlgScoreBan;

    @Schema(description = "客队半场进球数-算法值")
    @ExcelProperty("客队半场进球数-算法值")
    private String awayAlgScoreBan;

    @Schema(description = "主队全场进球数-算法值")
    @ExcelProperty("主队全场进球数-算法值")
    private String homeAlgScoreQuan;

    @Schema(description = "客队全场进球数-算法值")
    @ExcelProperty("客队全场进球数-算法值")
    private String awayAlgScoreQuan;

    @Schema(description = "主队半场进球数-推荐值")
    @ExcelProperty("主队半场进球数-推荐值")
    private String homePreScoreBan;

    @Schema(description = "客队半场进球数-推荐值")
    @ExcelProperty("客队半场进球数-推荐值")
    private String awayPreScoreBan;

    @Schema(description = "主队全场进球数-推荐值")
    @ExcelProperty("主队全场进球数-推荐值")
    private String homePreScoreQuan;

    @Schema(description = "客队全场进球数-推荐值")
    @ExcelProperty("客队全场进球数-推荐值")
    private String awayPreScoreQuan;

    @Schema(description = "推荐理由", example = "你猜")
    @ExcelProperty("推荐理由")
    private String remark;

    @Schema(description = "让球赔率", example = "")
    @ExcelProperty("让球赔率")
    private List<Map<String, Object>> letOddsList;

    @Schema(description = "欧洲赔率", example = "")
    @ExcelProperty("欧洲赔率")
    private List<Map<String, Object>> notLetOddsList;

    private String minusWinRate;
    private String minusDrawWinRate;
    private String minusVisitWinRate;
    /**
     * 升降幅度标识
     */
    private String minusWinUpDownFlag;
    private String minusDrawWinUpDownFlag;
    private String minusVisitWinUpDownFlag;
    /**
     * 进球差
     */
    private String ballDiff;
    private String ballDiffSort;

    /**
     * 半全场赔率
     */
    private String halfWholeOddList;
    private List<HalfScoreBallVo> halfWholeOddLists;
    /**
     * 比分赔率
     */
    private String scoreOddList;
    private List<List<HalfScoreBallVo>> scoreOddLists;
    /**
     * 进球赔率
     */
    private String ballOddList;
    private List<HalfScoreBallVo> ballOddLists;

    public String getBallDiff() {
        if (StrUtil.isNotEmpty(teamScore) && teamScore.indexOf(":") > 0) {
            try {
                String splitStr = "";
                if (teamScore.indexOf(":") > 0) {
                    splitStr = ":";
                }
                if (teamScore.indexOf("VS") > 0) {
                    splitStr = "VS";
                }
                String[] balls = teamScore.split(splitStr);
                String homeBall = balls[0];
                String awayBall = balls[1].substring(0, balls[1].indexOf("("));
                if(NumberUtil.isNumber(homeBall) && NumberUtil.isNumber(awayBall)) {
                    double diffValue = Double.parseDouble(homeBall) - Double.parseDouble(awayBall);
                    ballDiff = String.format("%.2f", diffValue);
                    ballDiffSort = String.format("%.2f", Math.abs(diffValue));
                }
            }catch (Exception e) {

            }
        }
        return ballDiff;
    }

    public String getMinusVisitWinRate() {
        if (StrUtil.isNotEmpty(visitWinRate) && StrUtil.isNotEmpty(letVisitWinRate)) {
            visitWinRate = visitWinRate.replace("(\uD83C\uDC04\uFE0F)", "");
            letVisitWinRate = letVisitWinRate.replace("(\uD83C\uDC04\uFE0F)", "");
            int v1 = calculateDifference(Double.parseDouble(visitWinRate) , Double.parseDouble(letVisitWinRate));
            minusVisitWinRate = String.valueOf( v1);
        }
        return minusVisitWinRate;
    }

    public String getMinusDrawWinRate() {
        if (StrUtil.isNotEmpty(drawWinRate) && StrUtil.isNotEmpty(letDrawWinRate)) {
            drawWinRate = drawWinRate.replace("(\uD83C\uDC04\uFE0F)", "");
            letDrawWinRate = letDrawWinRate.replace("(\uD83C\uDC04\uFE0F)", "");
            int v1 = calculateDifference(Double.parseDouble(drawWinRate) , Double.parseDouble(letDrawWinRate));
            minusDrawWinRate = String.valueOf( v1);
        }
        return minusDrawWinRate;
    }

    public String getMinusWinRate() {
        if (StrUtil.isNotEmpty(winRate) && StrUtil.isNotEmpty(letWinRate)) {
            winRate = winRate.replace("(\uD83C\uDC04\uFE0F)", "");
            letWinRate = letWinRate.replace("(\uD83C\uDC04\uFE0F)", "");

            int v1 = calculateDifference(Double.parseDouble(winRate) , Double.parseDouble(letWinRate));
            minusWinRate = String.valueOf( v1);
        }
        return minusWinRate;
    }

    public static int calculateDifference(double num1, double num2) {

        // 将两个数字转换为字符串，去掉小数点部分
        String str2 = String.format("%.6f", num2);
        String str1 = String.format("%.6f", num1);

        //如果两个数字都大于1，则直接相减
        if (num1 >=1 && num2 >= 1) {
            String dec1 = str1.substring(0,  str1.indexOf('.'));
            String dec2 = str2.substring(0, str2.indexOf('.'));

            return Integer.parseInt(dec2) - Integer.parseInt(dec1);
        }

        // 获取小数部分
        String dec1 = str1.substring(str1.indexOf('.') + 1);
        String dec2 = str2.substring(str2.indexOf('.') + 1);

        // 找到两个数字小数部分的第一位有效数字的位置
        int firstIndex1 = findFirstNonZero(dec1);
        int firstIndex2 = findFirstNonZero(dec2);

        int maxIndex = Math.max(firstIndex1, firstIndex2);

        //应该乘以10的倍数
        int big = getMappedValue(maxIndex);

        String dec11 = str1.substring(0, str1.indexOf('.') + 1) + dec1.substring(0, Math.min(dec1.length(), (maxIndex + 1)));
        String dec22 = str2.substring(0, str2.indexOf('.') + 1) + dec2.substring(0,Math.min(dec2.length(), (maxIndex + 1)));

        // 计算相减结果
        double result = Double.parseDouble(dec22) * big - Double.parseDouble(dec11) * big;

        // 根据规则四舍五入后取整数部分
        return (int) Math.round(result);
    }

    // 找到小数部分中的第一位有效数字的位置
    private static int findFirstNonZero(String decimal) {
        for (int i = 0; i < decimal.length(); i++) {
            if (decimal.charAt(i) != '0') {
                return i;
            }
        }
        return decimal.length();  // 如果没有有效数字，返回末尾
    }

    // 根据需要将数字放大倍数，调整小数点后的位数
    private static double scaleNumber(double num, int scale) {
        if (scale <= 0) return num;  // 不需要缩放
        return num * Math.pow(10, scale);  // 放大数值
    }

    // 使用 Math.pow 进行实现
    public static int getMappedValue(int n) {
        // Math.pow(10, n) 会返回 double 类型，需要转换为 int 类型
        return (int) Math.pow(10, n + 1);
    }

    private static String replaceShoot(String rate) {
        if (StrUtil.isEmpty(rate)) {
            return "";
        }
        return rate.replace("(\uD83C\uDC04\uFE0F)", "");
    }

    public String getScore() {
        return StrUtil.isEmpty(score)? "VS" : score;
    }

    public String getLetVisitWinRate() {
        return replaceShoot(letVisitWinRate);
    }

    public String getLetWinRate() {
        return replaceShoot(letWinRate);
    }

    public String getLetDrawWinRate() {
        return replaceShoot(letDrawWinRate);
    }

    public String getVisitWinRate() {
        return replaceShoot(visitWinRate);
    }

    public String getDrawWinRate() {
        return replaceShoot(drawWinRate);
    }

    public String getWinRate() {
        return replaceShoot(winRate);
    }

    public String[] getAwards() {
        //胜,胜,7+,胜-胜,6:1
        if (StrUtil.isNotEmpty(award)) {
            awards = award.replace("-", "").split(",");
            if (scoreOddLists != null && scoreOddLists.size() == 3) {
                //先看比分是胜还是平还是负
                if (StrUtil.isNotEmpty(score) && score.contains(":")) {
                    String[] scores = score.split(":");
                    int minus = Integer.parseInt(scores[0]) - Integer.parseInt(scores[1]);

                    if (minus == 0) {
                        List<HalfScoreBallVo> pingList = scoreOddLists.get(1);
                        //查看比分是否在列表里面
                        long count = pingList.stream().filter(i -> i.getDesc().equals(score)).count();
                        if (count == 0) {
                            awards[4] = "平其它";
                        }
                    }
                    if (minus > 0) {
                        List<HalfScoreBallVo> pingList = scoreOddLists.getFirst();
                        //查看比分是否在列表里面
                        long count = pingList.stream().filter(i -> i.getDesc().equals(score)).count();
                        if (count == 0) {
                            awards[4] = "胜其它";
                        }
                    }
                    if (minus < 0) {
                        List<HalfScoreBallVo> pingList = scoreOddLists.getLast();
                        //查看比分是否在列表里面
                        long count = pingList.stream().filter(i -> i.getDesc().equals(score)).count();
                        if (count == 0) {
                            awards[4] = "负其它";
                        }
                    }
                }
            }
        }
        return awards;
    }
}